define(function(require){
    var main = require('/static/main.js');
    var m = new main();
    function Cont(){
        this.Fun._init();
    }
    Cont.prototype.Fun = {
        taskid:$_GET['taskid'],
        _init:function(){
            this.bindEvent();
            this.getLevelList();
        },
        getLevelList:function(){
            var _this = this;
            $.ajax({
                type: "POST",
                data: {TaskId:_this.taskid},
                url: "/task/get-enterprise-employee-level-list",
                dataType: "json",
                success: function (ret) {
                    var str = '';
                    $.each(ret.Models,function(k,v){
                        if(k == 0){
                            str += ' <div class="checkbox-inline" style="padding-left: 0px;"><label class="cr-styled"><input type="checkbox" value="'+ v.MytipLevelId+'"><i class="fa"></i> '+ v.LevelName+'</label></div>';
                        }else{
                            str += '<div class="checkbox-inline"><label class="cr-styled"><input type="checkbox" value="'+ v.MytipLevelId+'"><i class="fa"></i> '+ v.LevelName+'</label></div>';
                        }
                    });
                    $(".MytipLevelList").html(str);
                    if(_this.taskid){
                        $.ajax({
                            type: "POST",
                            data: {TaskId:_this.taskid},
                            url: "/task/task-detail",
                            dataType: "json",
                            success: function (ret) {
                                var data = ret.Model
                                $(".TaskType").val(data.TaskType);
                                $(".TaskLevel").val(data.TaskLevel);
                                $(".Summary").val(data.Summary);
                                $(".TargetCnt").val(data.TargetCnt);
                                $(".BonusScore").val(data.BonusScore);
                                $.each(data.MytipLevelList,function(ke,va){
                                    $(".MytipLevelList .checkbox-inline").each(function (key, val) {
                                        var value = parseInt($(this).find("input").val());
                                        if(va.MytipLevelId == value){
                                            $(this).find("input").click()
                                        }
                                    });
                                })
                            }
                        })

                    }

                }
            });

        },
        bindEvent:function() {
            var _this = this;
            //完成
            $('.btn-info').click(function () {

                var JsonData = {
                    TaskType: $(".TaskType").val(),
                    Summary: $(".Summary").val().trim(),
                    TargetCnt: $(".TargetCnt").val(),
                    BonusScore: $(".BonusScore").val(),
                    TaskLevel: $(".TaskLevel").val(),
                    MytipLevelList: []
                }

                $(".MytipLevelList .checkbox-inline").each(function (k, v) {
                    if ($(this).find("input").is(":checked")) {
                        JsonData.MytipLevelList.push($(this).find("input").val());
                    }
                });
                if(!_this.seterrorMessage(JsonData.TaskType,'请选择任务类型') ||
                    !_this.seterrorMessage(JsonData.TaskLevel,'请选择任务级别') ||
                    !_this.seterrorMessage(JsonData.MytipLevelList,'请选择集客多多级别') ||
                    !_this.seterrorMessage(JsonData.Summary,'请输入任务描述') ||
                    !_this.seterrorMessage(JsonData.TargetCnt,'请输入目标数量') ||
                    !_this.seterrorMessage(JsonData.BonusScore,'请输入奖励积分')
                    ){
                    return false;
                }
                if( _this.taskid){
                    JsonData.TaskId = _this.taskid;
                }
                $.ajax({
                    type: "POST",
                    data: JsonData,
                    url: "/task/task-create",
                    dataType: "json",
                    success: function (ret) {
                        if (ret.IsBizSuccess) {
                            if( _this.taskid){
                                layer.msg('修改成功');
                            }else{
                                layer.msg('创建成功');
                            }

                            setTimeout(function(){
                            	window.location.href = "/task/index"
                            },1000)
                        } else {
                            layer.msg(ret.ErrMsg);
                        }
                        layer.close();
                    }
                })
            });
        },
        seterrorMessage:function(data,str){
            if(data.length < 1 || data == ''){
                $(".pass-msg.error").html(str);
                return false;
            }else{
                $(".pass-msg.error").html('');
                return true;
            }
        }
    }
    var a = new Cont();

})